﻿using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.Rendering;

public static partial class CommonFunction
{
    private static System.Random _random;
    public static System.Random Random
    {
        get
        {
            if (_random == null)
            {
                _random = new System.Random();
            }
            return _random;
        }
    }

    public static float NextFloat(this System.Random random, float vMin, float vMax)
    {
        int min = Mathf.RoundToInt(vMin * 100);
        int max = Mathf.FloorToInt(vMax * 100);
        int v = min;
        if (max > min)
        {
            v = random.Next(min, max);
        }
        return v * 0.01f;
    }

    public const string ConfigNullStr = "0";

    public static bool IsConfigStringNull(string str)
    {
        return string.IsNullOrEmpty(str) || str == ConfigNullStr;
    }

    public static string SetConfigStringToEmptyIf0(string str)
    {
        if (str == ConfigNullStr)
        {
            str = string.Empty;
        }
        return str;
    }

    public static string ListToString(List<int> strlist, char factor = ',')
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strlist.Count; i++)
        {
            sb.Append(strlist[i].ToString());
            sb.Append(factor);
        }
        return sb.ToString();
    }

    public static List<string> ToStringList(string str, char factor = ',')
    {        
        List<string> res = new List<string>();
        string[] strs = str.Split(factor);
        res.AddRange(strs);
        if (string.IsNullOrEmpty(res[res.Count - 1]))
        {
            res.RemoveAt(res.Count - 1);
        }
        return res;
    }

    public static List<int> ToIntList(string str, char factor = ',')
    {
        List<int> result = new List<int>();
        List<string> strList = ToStringList(str, factor);
        if (strList != null && strList.Count > 0)
        {
            int Len = strList.Count;
            for (int i = 0; i < Len; i++)
            {
                if (string.IsNullOrEmpty(strList[i]))
                    continue;
                int data = int.Parse(strList[i]);
                result.Add(data);
            }
        }
        return result;
    }

    public static List<uint> ToUintList(string str, char factor = ',')
    {
        List<uint> result = new List<uint>();
        List<string> strList = ToStringList(str, factor);
        if (strList != null && strList.Count > 0)
        {
            int Len = strList.Count;
            for (int i = 0; i < Len; i++)
            {
                if (string.IsNullOrEmpty(strList[i]))
                    continue;
                uint data = uint.Parse(strList[i]);
                result.Add(data);
            }
        }
        return result;

    }

    public static bool IsTouchedGUIObjects()
    {
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;

        List<RaycastResult> list = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, list);
        return list.Count > 0;
    }

    public static GameObject CreateGameObject(GameObject item, Transform parent)
    {
        if (item != null && parent != null)
        {
            GameObject obj = GameObject.Instantiate(item, parent);
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localScale = Vector3.one;
            obj.transform.localEulerAngles = Vector3.zero;
            return obj;
        }
        return null;
    }
    
    public static string RemoveCloneStr(string str)
    {
        int idx = str.LastIndexOf("(Clone)");
        if (idx >=0 )
        {
            return str.Substring(0, idx);
        }
        return str;
    }

    public static void SetLayer(Transform trans, int layer)
    {
        trans.gameObject.layer = layer;
        for (int i = 0; i < trans.childCount; i++)
        {
            SetLayer(trans.GetChild(i), layer);
        }
    }

    public static void SetCanvasGroupEnable(CanvasGroup group, bool enable)
    {
        group.alpha = enable ? 1 : 0;
        group.interactable = enable;
        group.blocksRaycasts = enable;
    }

    /// <summary>
    /// 获取XY平面，2D物体的旋转
    /// 默认图片向右为正
    /// </summary>
    /// <param name="direction"></param>
    public static Quaternion Get2DQuaternion(Vector3 direction)
    {
        //Mathf.Atan2含x=0的处理  无需自行处理
        return Quaternion.AngleAxis(Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg, Vector3.forward);
    }
}